package com.zzedu.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.zzedu.domain.ZzStuClassesR;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;


public interface ZzZzStuClassesRMapper extends BaseMapper<ZzStuClassesR> {

    @Select({
            "<script>",
            "SELECT ifnull(COUNT(*),0) AS count, class_id as classId",
            "FROM zz_stu_classes_r",
            "WHERE deleted=0 and stu_id IN (SELECT id FROM zz_student WHERE deleted = 0)",
            "  AND class_id IN",
            "  <foreach collection='classIdList' item='id' open='(' separator=',' close=')'>",
            "    #{id}",
            "  </foreach>",
            "GROUP BY class_id",
            "</script>"
    })
    List<Map<String, Object>> countStudentsByClassIds(@Param("classIdList") List<Long> classIdList);

    /**
     * 老师 学生 班级数量
     *
     * @param teacherId
     * @return
     */

    @Select({
            "<script>",
            "select count(*) clzzCount,sum(num) stuCount from zz_classes a\n" +
                    "left join(\n" +
                    "select class_id,count(*) num from zz_stu_classes_r\n" +
                    "where deleted=0 and stu_id in(select id from zz_student where deleted=0)\n" +
                    "GROUP BY class_id\n" +
                    ") b on a.id=b.class_id \n" +
                    "where a.deleted=0 and a.teacher_id=#{teacherId}",
            "</script>"
    })
    Map<String, Object> studentCountAndClzzCountByTeacherId(@Param("teacherId") Long teacherId);
}
